var currentpage = 1, totalpage = 1;
$(function(){
	//获取设置课程名
	$.ajax({
		url: "publishlessons/" + LessonID + "/" + Phase + "/lessoninfo",
		type: "GET",
		contentType: "application/json",
		success: function (data) {
			$("#courseAvatar").attr("src", data.image);//封面
			$("#courseAvatar").height($("#courseAvatar").width() / 1.8);
			var lang = /zh/.test(getCurrentLang());
			var str = '<span class="tag lessontypetag">' + (lang ? data.typenamecn : data.typenameen) + '</span> ';
			str += data.phase && data.phase != 1 ? (' <span class="tag lessonpstag">' + langThis("cm_coursePhase").replace("{p}", data.phase) + '</span> ') : '';
			str += ' <span class="tag lessonenddate">' + langThis("rm_csEndTime") + ' : ' + data.enddate + '</span></div><p>' + data.description + '</p>';
			$("#courseIntro").html("<h4>" + data.lessonname + "</h4>" + str);//描述
			$("#courseIntro").css("max-height", $("#courseAvatar").height() + "px");
			$.setHead=function(name,des){
				if(arguments.length)
				{
					$("#courseIntro>h4").html(name);
					$("#courseIntro>p").html(des);
				}else{
					$("#courseIntro>h4").html(data.lessonname);
					$("#courseIntro>p").html(data.description);
				}
			};
			//切换按钮
			$("span.myswitch-inner").attr("data-on",langThis("CSC_OrderByLab"));
			$("span.myswitch-inner").attr("data-off",langThis("nav_student"));
			$("#onoffswitch").click(function(){
				if(this.checked){ // 实验
					//border样式
					$("span.myswitch-switch").attr("class","myswitch-switch myswitch-borderL");
					$("label.myswitch-label").attr("class","myswitch-label myswitch-borderL");
					fillLabList();
				}else{ // 学生
					$("span.myswitch-switch").attr("class","myswitch-switch myswitch-borderS");
					$("label.myswitch-label").attr("class","myswitch-label myswitch-borderS");
					fillStuList();
				}
			});
			// hover样式
			$("div.myswitch").hover(function(){
				if(this.children[0].checked){
					$("span.myswitch-inner").attr("class","myswitch-inner myswitch-hoverL");
				}else{
					$("span.myswitch-inner").attr("class","myswitch-inner myswitch-hoverS");
				}
			},function(){
				$("span.myswitch-inner").attr("class","myswitch-inner");
			});
		},
		error: function (event) {
			layer.msg(langThis("role_optionfaild"), { icon: 2 });
			$('#container img[alt="loading"]').fadeOut();
		}
	});
	fillStuList();
});
// 填充实验列表
function fillLabList()
{
	$('#container img[alt="loading"]').fadeIn();
	$("#partpagenavbar").fadeOut(100); // 翻页条
	// 设置表头
	var TableHead="<tr><th>";
	TableHead+=langThis("lab_labName")+"</th><th>";
	TableHead+=langThis("labMg_labContent")+"</th><th>";
	TableHead+=langThis("CSC_CommentOrNot")+"</th><th>";
	TableHead+=langThis("labMg_handle")+"</th></tr>";
	$("#TableHead").html(TableHead);
	$.ajax({
		url: "homework/" + LessonID + "/" + Phase + "/lablist",
		type: "POST",
		contentType: "application/json",
		data: JSON.stringify({
			// pageindex: currentpage
		}),
		dataType: "json",
		success: function (data) {
			totalpage=1; // 实验不分页
			if(data.rows.length)
			{
				var R=data.rows;
				showlabstulstpage.LabINFO=R;
				var lablist="",done,undone;
				for(var i in R)
				{
					lablist+='<tr>';
					lablist+='<td>'+R[i].name+'</td>';
					lablist+='<td>'+R[i].description+'</td>';
					done=R[i].done;
					if(!done || done=="null"){done=0;}
					undone=R[i].total-done;
					lablist+='<td><span class="'+(undone==0?'green">0':'red">'+undone)+'</span>/'+done+'</td>';
					lablist+='<td onclick="showlabstulstpage(\''+R[i].id+'\')"><a>'+langThis("lc_checkresult")+'</a></td></tr>';
				}
				$("#uncorrectedlisttbody").html(lablist);
			}else{
				$("#uncorrectedlisttbody").html("<tr><td colspan='4'>"+langThis("CSS_noSudent")+"</td></tr>");
			}
			$('#container img[alt="loading"]').fadeOut();
		},
		error: function (event) {
			$("#uncorrectedlisttbody").html("<tr><td colspan='4'>"+langThis("role_optionfaild")+"</td></tr>");
			layer.msg(langThis("role_optionfaild"), { icon: 2 });
			$('#container img[alt="loading"]').fadeOut();
		}
	});
}
// 填充学生列表
function fillStuList()
{
	$('#container img[alt="loading"]').fadeIn();
	// 设置表头
	var TableHead="<tr><th>";
	TableHead+=langThis("sm_studentname")+"</th><th>";
	TableHead+=langThis("ms_sex")+"</th><th>";
	TableHead+=langThis("pm_school")+"</th><th>";
	TableHead+=langThis("ms_major")+"</th><th>";
	TableHead+="<p data-toggle='tooltip' data-placement='top' title='"+langThis("CSC_edun")+"'>"+langThis("CSC_Statistics")+"</p></th><th>";
	TableHead+=langThis("labMg_handle")+"</th></tr>";
	$("#TableHead").html(TableHead);
	$('#TableHead p[data-toggle="tooltip"]').tooltip();
	$.ajax({
		url: "homework/" + LessonID + "/" + Phase + "/studentlist",
		type: "POST",
		contentType: "application/json",
		data: JSON.stringify({
			pageindex: currentpage
		}),
		dataType: "json",
		success: function (data) {
			totalpage=Math.ceil(data.total);
			if(data.rows.length)
			{
				var R=data.rows;
				showstuworklstpage.stuINFO=R;
				var stulist="",done,undone,Sundone;
				for(var i in R)
				{
					stulist+='<tr>';
					stulist+='<td>'+R[i].name+'</td>';
					switch (R[i].sex.toString()) {
						case "1": stulist += '<td>' + langThis("ms_male") + '</td>'; break;
						case "2": stulist += '<td>' + langThis("ms_female") + '</td>'; break;
						default: stulist += '<td>' + langThis("ms_malefemale") + '</td>'; break;
					}
					stulist+='<td>'+R[i].university+'</td>';
					stulist+='<td>'+R[i].major+'</td>';
					done=R[i].homeworkdone;
					if(!done || done=="null"){done=0;}
					undone=R[i].tatal-done;
					Sundone=(!R[i].scoredone||R[i].scoredone=="null")?R[i].tatal:R[i].tatal-R[i].scoredone;
					stulist+="<td><p data-toggle='tooltip' data-placement='top' title='"+langThis("CSC_edun")+"'>";
					stulist+='<span class="'+(Sundone==0?'green">0':'red">'+Sundone)+'</span> / <span class="'+(undone==0?'green">0':'red">'+undone)+'</span> / '+done+'</p></td>';
					stulist+='<td onclick="showstuworklstpage(\''+R[i].id+'\')"><a>'+langThis("lc_checkresult")+'</a></td></tr>';
				}
				$("#uncorrectedlisttbody").html(stulist);
				$('#uncorrectedlisttbody p[data-toggle="tooltip"]').tooltip();
				setpartpagebar();
			}else{
				$("#uncorrectedlisttbody").html("<tr><td colspan='6'>"+langThis("CSS_noSudent")+"</td></tr>");
			}
			$('#container img[alt="loading"]').fadeOut();
		},
		error: function (event) {
			$("#uncorrectedlisttbody").html("<tr><td colspan='6'>"+langThis("role_optionfaild")+"</td></tr>");
			layer.msg(langThis("role_optionfaild"), { icon: 2 });
			$('#container img[alt="loading"]').fadeOut();
		}
	});
}
function showlabstulstpage(labid)
{
	$('#container img[alt="loading"]').fadeIn();
	if(arguments.length){
		// 设置标题&隐藏切换按钮
		$("div.myswitch").fadeOut(100);
		$("#RETURNLABLIST").fadeIn(100); //返回按钮
		var LabINFO=null;
		for(var i in showlabstulstpage.LabINFO)
		{
			if(showlabstulstpage.LabINFO[i].id==labid){
				LabINFO=showlabstulstpage.LabINFO[i];
				break;
			}
		}
		$.setHead(LabINFO.name,LabINFO.description);
		// 设置表头
		var TableHead="<tr><th>";
		TableHead+=langThis("sm_studentname")+"</th><th>";
		TableHead+=langThis("pm_school")+"</th><th>";
		TableHead+=langThis("ms_major")+"</th><th>";
		TableHead+=langThis("CSC_workLink")+"</th><th>";
		TableHead+=langThis("lc_score")+"</th><th>";
		TableHead+=langThis("CSC_Comment")+"</th><th>";
		TableHead+=langThis("labMg_handle")+"</th></tr>";
		$("#TableHead").html(TableHead);
		// 填充内容
		$.ajax({
			url: "homework/" + labid + "/" + LabINFO.lessonid +"/" + LabINFO.phase + "/studentall",
			type: "GET",
			contentType: "application/json",
			success: function (data) {
				if(data&&data.length)
				{
					var stulist="",workurl="",OP="pm_modify";
					for(var i in data){
						stulist+="<tr><td>"+data[i].name;
						stulist+="</td><td>"+data[i].university;
						stulist+="</td><td>"+data[i].major;
						stulist+="</td><td class='urlTD'>"
						workurl=data[i].url;
						if(!workurl||workurl==null||workurl==""){
							stulist+="<span class='red'>"+langThis("lc_unsubmitresult")+"</span>";
						}else{
							stulist+="<a href='"+data[i].url+"' target='_blank'>"+data[i].url+"</a>";
						}
						stulist+="</td><td class='scoreCol'><input id='"+labid+"' type='number' value='"+data[i].teacherscore+"'/></td>";
						stulist+="<td><textarea>"+(data[i].teacherremark==null?"":data[i].teacherremark)+"</textarea></td>";
						if(data[i].teacherscore==null||data[i].teacherscore==""||data[i].teacherremark==null||data[i].teacherremark==""){
							OP="ol_submit";
						}
						stulist+="</td><td><a data-stuid='"+data[i].id+"' class='MOS'>"+langThis(OP)+"</a></td><tr>";
					}
					$("#uncorrectedlisttbody").html(stulist);
					// 成绩评语
					Correct("#uncorrectedlisttbody td>a.MOS");
				}else{
					$("#uncorrectedlisttbody").html("<tr><td colspan='7'>"+langThis("CSS_noSudent")+"</td></tr>");
				}
				$('#container img[alt="loading"]').fadeOut();
			},
			error: function (event) {
				$("#uncorrectedlisttbody").html("<tr><td colspan='7'>"+langThis("role_optionfaild")+"</td></tr>");
				layer.msg(langThis("role_optionfaild"), { icon: 2 });
				$('#container img[alt="loading"]').fadeOut();
			}
		});
	}else{
		// 返回的时候改回去
		$("#RETURNLABLIST").fadeOut(100);
		$.setHead();
		fillLabList();
		$("div.myswitch").fadeIn(100);
	}
}
//显隐填充学生作业列表
function showstuworklstpage(studentid){
	$('#container img[alt="loading"]').fadeIn();
	if(typeof(studentid)=="undefined"){
		$("#studentworklist").hide();
		$("#courseworkcrt").fadeIn(100);
		$("#partpagenavbar").fadeIn(100);
		gotopage(currentpage);
		$('#container img[alt="loading"]').fadeOut();
	}else{
		$("#courseworkcrt").hide();
		$("#partpagenavbar").hide();
		//填充学生信息
		var stuINFO;
		for(var stu in showstuworklstpage.stuINFO)
		{
			stuINFO=showstuworklstpage.stuINFO[stu];
			if(studentid==stuINFO.id)
			{
				$("#stuavatar").attr("src","./photos/" + stuINFO.photo);
				$("#stunickname").text(stuINFO.nickname);
				$("#stuuniversity").text(stuINFO.university);
				$("#stuname").text(stuINFO.name);
				$("#stumajor").text(stuINFO.major);
				break;
			}
		}
		// 填充结果
		$.ajax({
			url: "homework/" + studentid + "/" + LessonID + "/"+Phase+"/laball",
			type: "GET",
			contentType: "application/json",
			success: function (data) {
				if(data.length)
				{
					labList="";
					var OP="pm_modify";
					for(var i in data)
					{
						labList+="<tr><td><p data-toggle='tooltip' data-placement='top' title='"+data[i].description+"'>"+data[i].name+"</p></td>";
						labList+="<td class='urlTD'>";
						if(data[i].url && data[i].url!="null")
						{
							labList+="<a href='"+data[i].url+"' target='_blank'>"+data[i].url+"</a></td>";
						}else{
							labList+="<span class='red'>"+langThis("lc_unsubmitresult")+"</span></td>";
						}
						labList+="<td class='scoreCol'><input id='"+data[i].id+"' type='number' value='"+data[i].teacherscore+"'/></td>";
						labList+="<td><textarea>"+(data[i].teacherremark==null?"":data[i].teacherremark)+"</textarea></td>";
						if(data[i].teacherscore==null||data[i].teacherscore==""||data[i].teacherremark==null||data[i].teacherremark==""){
							OP="ol_submit";
						}
						labList+="<td><a data-stuid='"+studentid+"' class='MOS'>"+langThis(OP)+"</a></td></tr>";
					}
					$("#homeworklistbody").html(labList);
					Correct("#homeworklistbody td>a.MOS");
				}else{
					$("#homeworklistbody").html("<tr><td colspan='5'>"+langThis("CSS_noLab")+"</td></tr>");
				}
				$('#container img[alt="loading"]').fadeOut();
				$('#homeworklistbody p[data-toggle="tooltip"]').tooltip();
			},
			error: function (event) {
				$("#homeworklistbody").html("<tr><td colspan='5'>"+langThis("role_optionfaild")+"</td></tr>");
				layer.msg(langThis("role_optionfaild"), { icon: 2 });
				$('#container img[alt="loading"]').fadeOut();
			}
		});
		$("#studentworklist").fadeIn(100);
	}
}
// 注册事件提交/修改分数
function Correct(ASELECTOR){
	$(ASELECTOR).click(function(){
		var me=this;
		var TR=$($(me).parent()).parent();
		var SC=TR.find("td.scoreCol>input");
		var score=SC.val();
		var remark=TR.find("td>textarea").val().trim().replace(/</g,'&lt;').replace(/>/g,'&gt;');
		if(score==""&&remark==""){
			layer.msg(langThis("myinfo_lgNameEmpty"), { icon: 2 });
			$(me).html($(me).text().trim()+' <span class="glyphicon glyphicon-remove red"/>');
			return;
		}
		$.ajax({
			url: "studentscore",
			type: "POST",
			contentType: "application/json",
			data: JSON.stringify([{
				userinfoid: $(me).attr("data-stuid"),
				labid:SC.attr("id"),
				score:score,
				remark:remark
			}]),
			dataType: "json",
			success: function(){
				layer.msg(langThis("mi_OprationSucess"), { icon: 1 });
				$(me).html($(me).text().trim()+' <span class="glyphicon glyphicon-ok green"/>');
			},
			error:function() {
				layer.msg(langThis("role_optionfaild"), { icon: 2 });
				$(me).html($(me).text().trim()+' <span class="glyphicon glyphicon-remove red"/>');
				$('#container img[alt="loading"]').fadeOut();
			}
		});
	});
}
//分页控制
function setpartpagebar(){
	var pagesli='';
	//首页、上一页
	if(currentpage==1 || totalpage==1){
		pagesli+='<li title="'+langThis("nav_master")+'" class="active"><a>&laquo;</a></li>';
		pagesli+='<li title="'+langThis("nav_pre")+'" class="active"><a>&lt;</a></li>';
	}else{
		pagesli+='<li title="'+langThis("nav_master")+'"><a style="cursor:pointer;" onclick="gotofirstpage()">&laquo;</a></li>';
		pagesli+='<li title="'+langThis("nav_pre")+'"><a style="cursor:pointer;" onclick="previouspage()">&lt;</a></li>';
	}
	//页码
	var startpage;
	if (currentpage-5<=0){
		startpage=1;
	}else if(currentpage+5>totalpage){
		startpage=totalpage-9;
	}else{
		startpage=currentpage-4;
	}
	// 左过长
	if (startpage>1){
		pagesli += '<li><a style="cursor:pointer;" onclick="gotopage(' + (startpage-1) + ')">...</a></li>';
	}else{
		startpage=1;
	}
	for(var j=startpage;j<=totalpage;j++){
		if(j==currentpage){
			pagesli+='<li class="active"><a>'+j+'</a></li>';
		}else{
			pagesli+='<li><a style="cursor:pointer;" onclick="gotopage('+j+')">'+j+'</a></li>';
		}
		//右过长
		if((j==startpage+9) && (j<totalpage)){
			pagesli+='<li><a style="cursor:pointer;" onclick="gotopage('+(startpage+10)+')">...</a></li>';
			break;
		}
	}
	//下一页、尾页
	if(currentpage==totalpage || totalpage==1){
		pagesli+='<li title="'+langThis("nav_next")+'" class="active"><a>&gt;</a></li>';
		pagesli+='<li title="'+langThis("nav_last")+'" class="active"><a>&raquo;</a></li>';
	}else{
		pagesli+='<li title="'+langThis("nav_next")+'"><a style="cursor:pointer;" onclick="nextpage()">&gt;</a></li>';
		pagesli+='<li title="'+langThis("nav_last")+'"><a style="cursor:pointer;" onclick="gotolastpage()">&raquo;</a></li>';
	}
	$("#partpagenavbar>ul.pagination").html(pagesli);
	$("#partpagenavbar").fadeIn();
	// 子页面设置父页面iframe高度
    if(window.parent.setiframeHeight){
        var ht=$("#container").height();
		ht=ht>800?ht:800;
        window.parent.setiframeHeight(ht);
    }
}
function previouspage(){
	currentpage--;
	fillStuList();
}
function nextpage(){
	currentpage++;
	fillStuList();
}
function gotopage(pagenum){
	currentpage=pagenum;
	fillStuList();
}
function gotofirstpage(){
	currentpage=1;
	fillStuList();
}
function gotolastpage(){
	currentpage=totalpage;
	fillStuList();
}